package thread_design;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/5/25
 * Time:20:46
 * 懒汉方式v3--最终版
 **/
public class Demo5 {

    static class Singleton {
        //创建一个私有的构造函数，防止其他类直接实例化
        private Singleton() {

        }
        //创建私有的类对象
        private static volatile Singleton singleton = null;
        //提供统一的访问方法
        public static Singleton getInstance(){
            if (singleton == null){
                synchronized (Singleton.class) {
                    if (singleton == null){
                        singleton = new Singleton();
                    }
                }
            }
            return singleton;
        }
    }

    private static Singleton s1 = null;
    private static Singleton s2 = null;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                s1 = Singleton.getInstance();
            }
        });
        t1.start();

        s2 = Singleton.getInstance();

        t1.join();

        System.out.println(s1==s2);
    }


    /*static class Singleton {
        //1.先创建一个私有的构造函数,防止其他类直接实例化
        private Singleton(){

        }
        //2.创建私有的类对象 （volatile）
        private static volatile Singleton singleton = null;

        //3.提供统一的访问方法
        public static Singleton getInstance() {
            if(singleton==null) {//第一次访问
                synchronized (Singleton.class) {
                    if (singleton == null) {
                        singleton = new Singleton();
                    }
                }
            }
            return singleton;
        }
    }

    private static Singleton s1 = null;
    private static Singleton s2 = null;

    public static void main(String[] args) throws InterruptedException {
        //创建新线程执行任务
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                s1 = Singleton.getInstance();
            }
        });
        t1.start();

        //使用主线程执行任务
        s2 = Singleton.getInstance();

        t1.join();

        System.out.println(s1==s2);
    }*/

    /*static class Singleton {
        //1.先创建一个私有的构造函数，防止其他类直接实例化
        private Singleton(){

        }
        //2.创建私有的类对象
        private static volatile Singleton singleton = null;
        //3.提供统一的访问方法
        public static Singleton getInstance(){
            if (singleton == null){
                synchronized (Singleton.class) {
                    if (singleton == null) {
                        singleton = new Singleton();
                    }
                }
            }
            return singleton;
        }
    }


    private static Singleton s1 = null;

    public static void main(String[] args) throws InterruptedException {
        //创建多线程执行任务
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                2,2,0, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(2)
        );

        for (int i = 0; i < 2; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    s1 = Singleton.getInstance();
                }
            });
        }

        //使用主线程执行任务
        Singleton s2 = Singleton.getInstance();

        System.out.println(s1== s2);
    }*/
}
